#include <router/RouterControlClient.h>
#include <iostream>
#include <util/forward_rule.h>
using namespace std;

void usage()
{
    cout << "usage: routerctl op src_port dst_user dst_host target_ip target_port" << endl;
    cout << "op:              " << endl;
    cout << "    create_rule  " << endl;
    cout << "    delete_rule  " << endl;
}

int main(int argc, char** args)
{
    if (argc != 7){
        usage();
        return -1;
    }

    RouterControlClient router_ctl_client("127.0.0.1", 8970);

    forward_rule rule;
    rule.src_port = atoi(args[2]);
    rule.dst_user = atoi(args[3]);
    rule.dst_host = atoi(args[4]);
    rule.target_port = atoi(args[6]);

    if (inet_pton(AF_INET, args[5], &rule.target_ip) == -1){
        perror("不是一个有效的IP地址\n");
        exit(-1);
    }
    rule.target_ip = ntohl(rule.target_ip);

    if (strcmp(args[1], "create_rule") == 0){
        bool result = router_ctl_client.create_forward_rule(rule);
        cout << (result ? "转发规则创建成功" : "转发规则创建失败") << endl;
    }
    else if (strcmp(args[2], "delete_rule") == 0){
        router_ctl_client.delete_forward_rule(rule);
    }
    else {
        usage();
    }
    return 0;
}